package com.hitqz.robot.biz.flow.component.oilSample;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.enums.TicketsFormFieldEnum;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.business.dispatch.DispatchService;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2024/11/21
 */
@LiteflowComponent(value = "GoDevicePrePosComponent", name = "[取油样机器人]前往设备点位前置点")
@RequiredArgsConstructor
@Slf4j
public class GoDevicePrePosComponent extends NodeComponent {


    private final RobotDriverService robotDriverService;

    private final RobotService robotService;

    private final RobotMapPosService robotMapPosService;

    private final DispatchService dispatchService;

    private final RobotEquipmentService robotEquipmentService;

    @Override
    public void process() throws Exception {
        FlowContextDto dto =  this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        Robot robot = robotService.getDefaultRobot();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String deviceKey = "";
        for (String key : ticketsDto.getModalData().keySet()) {
            if (key.contains(TicketsFormFieldEnum.EQUIPMENT_LIST.getFieldName())) {
                deviceKey = key;
                break;
            }
        }
        String device = String.valueOf(ticketsDto.getModalData().get(deviceKey));
        RobotEquipment robotEquipment = robotEquipmentService.getById(Integer.valueOf(device));
        RobotMapPos robotMapPos = robotMapPosService.getById(Integer.valueOf(robotEquipment.getBindPosId()));

        RobotMapPos prePos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                .like(RobotMapPos::getPreCode, robotMapPos.getCode())
                .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        log.info("前往起始目标点{}", prePos.getCode());
        boolean isSuccess = dispatchService.go2TargetPosition(prePos, false);
        //
        TimeUnit.SECONDS.sleep(3);
        IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        while (iRobotPosDto == null || !iRobotPosDto.getCurrentStation().equals(prePos.getCode()) || (!iRobotPosDto.isReached())) {
            iRobotPosDto = robotDriverService.getWebPos();
        }
        log.info("起始点机器人到达位置了");

    }
}
